package edu.sctu.lab04.listener;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.Serializable;

/**
 * 监听器接口
 *
 * ServletContextListener
 * ServletContextAttributeListener
 * HttpSessionListener
 * HttpSessionAttributeListener
 * HttpSessionActivationListener
 * ServletRequestListener
 * ServletRequestAttributeListener
 * HttpSessionBindingListener
 *
 */

public class UserListener implements
        ServletContextListener,
        HttpSessionListener,
        ServletRequestListener,
        ServletContextAttributeListener,
        HttpSessionAttributeListener,
        ServletRequestAttributeListener,
        HttpSessionBindingListener,
        HttpSessionActivationListener,
        Serializable{

    //ServletContextListener

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        String name = sce.getServletContext().getInitParameter("name");
        System.out.println("name:"+name);
        System.out.println("启动服务器，Context初始化！");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

        System.out.println("服务器停止，Context销毁！");
    }


    //HttpSessionListener

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("sessionCreated:session被创建！");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("sessionDestroyed：session被销毁！");
    }


    //ServletRequestListener

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("requestInitialized:request初始化！");
    }


    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("requestInitialized:request销毁！");

    }


    /*监听域对象中属性的增加和删除的事件监听器*/


    //ServletContextAttributeListener

    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("ServletContextAttribute:添加attribute,"+scae.getName());

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("ServletContextAttribute:移除attribute,"+scae.getName());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("ServletContextAttribute:替换attribute,"+scae.getName());
    }


    //HttpSessionAttributeListener

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("HttpSessionAttribute:添加attribute,"+se.getName());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("HttpSessionAttribute:移除attribute,"+se.getName());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("HttpSessionAttribute:替换attribute,"+se.getName());
    }

    //ServletRequestAttributeListener


    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequestAttribute:添加attribute,"+srae.getName());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequestAttribute:移除attribute,"+srae.getName());
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequestAttribute:替换attribute,"+srae.getName());
    }

    /*监听绑定到HttpSession域中某个对象状态的事件监听器*/
    /*
    * 这类监听器主要监听的是绑定到HttpSession域中某个对象状态的事件，
    * HttpSession中对象的状态有两种：绑定与解除绑定、钝化与活化。
    * 所谓的绑定与解除绑定，就是指在HttpSession中将某个对象设置为属性值或者移除某个属性的值。
    * 而钝化是指服务器会将不常使用的Session对象暂时序列化到系统文件或数据库中，
    * 而活化就是将暂存在系统文件或数据库中的Session对象反序列化到服务器中，
    * 当Tomcat服务器被关闭或者重启时，Tomcat会将Session对象钝化到服务器文件系统中，
    * 当服务器被重新加载时，Session对象也会被钝化。
    * 要实现这两种的监听器，就需要继承HttpSessionBindingListener和HttpSessionActivationListener，
    * */


    //HttpSessionBindingListener

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("valueBound:name="+event.getName());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("valueUnbound:name="+event.getName());
    }


    //HttpSessionActivationListener

    @Override
    public void sessionWillPassivate(HttpSessionEvent se) {
        System.out.println("sessionWillPassivate : name=" + se.getSource());

    }

    @Override
    public void sessionDidActivate(HttpSessionEvent se) {
        System.out.println("sessionDidActivate : name=" + se.getSource());

    }
}
